<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="活动大类名称">
          <a-input placeholder="请输入活动大类名称" v-decorator="['categoriesName', validatorRules.categoriesName]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="账务费用归类">
          <cn-dict-select-tag
            placeholder="请选择账务费用归类"
            dict-code="financial_fee"
            triggerChange
            :options="{value:'dictDataCode',label:'dictDataDesc'}"
            v-decorator.trim="[ 'erpCostType', validatorRules.erpCostType]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="业务费用归类">
          <cn-dict-select-tag
            placeholder="请选择业务费用归类"
            dict-code="business_fee"
            triggerChange
            :options="{value:'dictDataCode',label:'dictDataDesc'}"
            v-decorator.trim="[ 'businessCostType', validatorRules.businessCostType]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="关联预算科目名称">
          <j-select-subjects placeholder="请选择关联预算科目名称"
                             :multiple="false" v-decorator="['subjectsCode', validatorRules.subjectsCode]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="关联活动细类">
          <j-select-categories-detail-no placeholder="请选择关联活动细类"
                                      :multiple="true" v-decorator="['categoriesDetailCode', validatorRules.categoriesDetailCode]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注">
          <a-textarea placeholder="请输入备注" v-decorator="['remarks', {}]"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import {httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import JSelectCategoriesDetailNo from '../../../../components/cn-select-popup/JSelectCategoriesDetailNo'
  import JSelectSubjects from '@/components/cn-select-popup/JSelectSubjects'

  export default {
    name: "TpmActCategoriesModal",
    components: {JSelectCategoriesDetailNo, JSelectSubjects},
    data() {
      return {
        title: "操作",
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          categoriesName: {rules: [{required: true, message: '请输入活动大类名称!'}]},
          erpCostType: {rules: [{required: true, message: '请选择账务费用归类!'}]},
          businessCostType: {rules: [{required: true, message: '请选择业务费用归类!'}]},
          subjectsCode: {rules: [{required: true, message: '请选择关联预算科目!'}]},
          categoriesDetailCode: {rules: [{required: true, message: '请选择关联活动细类!'}]}
        },
        url: {
          add: window._CONFIG['domianTpmURL'] + "/act/tpmActCategories/saveOrUpdate",
          edit: window._CONFIG['domianTpmURL'] + "/act/tpmActCategories/saveOrUpdate",
        },
      }
    },
    created() {
    },
    methods: {
      add() {
        this.edit({});
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'createName', 'createDate', 'createDateTime', 'updateDate',
            'updateDateTime', 'updateName', 'status', 'deleteStatus', 'remarks', 'saasid', 'categoriesCode', 'categoriesName',
            'erpCostType', 'businessCostType', 'subjectsCode', 'subjectsName', 'categoriesDetailCode'))
          //时间格式化
        });

      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            httpurl += this.url.add;
            method = 'post';
            let formData = Object.assign(this.model, values);
            //时间格式化

            console.log(formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })


          }
        })
      },
      handleCancel() {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>